package cn.neusoft.staticproxy;

/**
 * 代理类必须和目标类实现相同的接口
 */
public class EmpServiceProxy implements IEmpService{

    IEmpService target;   //传入目标类的实例   new EmpService()
    public EmpServiceProxy(IEmpService target){
        this.target = target;
    }

    @Override
    public void add() {
        System.out.println("开启事务");
        try{
            //核心业务由目标类来完成
            target.add();
            System.out.println("提交事务");
        }catch (Exception e){
            System.out.println("事务回滚");
        }finally {
            System.out.println("关闭事务");
        }
    }

    @Override
    public void update() {
        System.out.println("开启事务");
        try{
            target.update();
            System.out.println("提交事务");
        }catch (Exception e){
            System.out.println("事务回滚");
        }finally {
            System.out.println("关闭事务");
        }
    }
}
